package mysql

import (
	"fmt"
	"go.uber.org/zap"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"web_app/setting"
)

var GormDB *gorm.DB

func Init(mysqlCofig *setting.MysqlConfig) error {

	dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=%v&parseTime=True&multiStatements=true&loc=Local",
		mysqlCofig.User,
		mysqlCofig.Password,
		mysqlCofig.Host,
		mysqlCofig.Port,
		mysqlCofig.Dbname,
		mysqlCofig.Charset,
	)
	var err error
	GormDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		zap.L().Error("连接数据库失败", zap.Error(err))

	}

	return err
}
